package com.rub.android

import android.content.Context
import android.view.View
import android.widget.Toast
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.Target
import com.lzy.okgo.OkGo
import org.litepal.LitePal
import java.io.File
import com.rub.android.base.BaseActivity
import com.rub.android.jetpack.*
import com.rub.android.widget.ConfigManager
import com.rub.android.ui.activity.*
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : BaseActivity() {

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    /**
     * 重写
     */
    override fun initListener() {

        context = this
        //初始化LitePal
        LitePal.initialize(this)
        //读取下视频源的配置
        ConfigManager.sourceConfigs
        //必须调用初始化
        OkGo.getInstance().init(application)

        setOnClickListener(btnTv, this, TextViewActivity::class.java)
        setOnClickListener(btnOne, this, ButtonActivity::class.java)
        setOnClickListener(btnCamera, this, CameraActivity::class.java)
        setOnClickListener(btnAppAttr, this, AppAttrActivity::class.java)
        setOnClickListener(btnVideo, this, VideoPlayerActivity::class.java)
        setOnClickListener(btnExoPlayer, this, ExoPlayerActivity::class.java)
        setOnClickListener(btnVideoPlayer, this, VideoPlayerDetailActivity::class.java)
        setOnClickListener(btnGlideImg, this, GlideImageActivity::class.java)
        setOnClickListener(btnLogin, this, LoginActivity::class.java)
        setOnClickListener(btnRvA, this, RecyclerViewAdapterActivity::class.java)
        setOnClickListener(btnNoti, this, NotificationActivity::class.java)
        setOnClickListener(btnListView, this, ListViewActivity::class.java)
        setOnClickListener(btnFg, this, FragmentActivity::class.java)
        setOnClickListener(btnTags, this, TagsActivity::class.java)
        setOnClickListener(btnHome, this, HomeActivity::class.java)
        setOnClickListener(btnChr, this, ChronometerActivity::class.java)
        setOnClickListener(btnBind, this, DataBinding2Activity::class.java)
        setOnClickListener(btnNav, this, NavigateActivity::class.java)
        setOnClickListener(btnWm, this, WorkManagerActivity::class.java)
        setOnClickListener(btnSplash, this, SplashActivity::class.java)
        setOnClickListener(btnGithub, this, GithubActivity::class.java)
    }


    /**
     * Glide 下载图片
     */
    fun downloadImage(view: View?) {
        Thread {
            try {
                val url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg"
                val context = applicationContext
                val target = Glide.with(context)
                    .load(url)
                    .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                val imageFile: File = target.get()
                //Android必须在子线程才不会奔溃
                runOnUiThread {
                    Toast.makeText(context, imageFile.path, Toast.LENGTH_LONG).show()
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }.start()
    }


    companion object {
        //精态变量
        lateinit var context: Context
    }

}